module testapps.TestShaders;

import openglutils.GL;
import openglutils.GLU;
import openglutils.SDL;
import openglutils.GLShader;
import openglutils.GLProgram;

import testapps.TestApp;
import testapps.Box;

import tango.io.Stdout;

void main(char[][] args)
{
	auto test = new TestShaders();
	test.createGLWindow("Test Shaders", 800, 600, 32, false);
	test.initGL();
	test.loadShaders();
	test.run();
	delete test;
}

public class TestShaders: TestApp
{
	private GLProgram program;
	private GLVertexShader vertShader;
	private GLFragmentShader fragShader;

	private Box box;
	
	public this()
	{
		box = new Box();
	}
	
	public ~this()
	{
		program.detachShader(fragShader);
		program.detachShader(vertShader);
		delete fragShader;
		delete vertShader;
		delete program;
	}
	
	public void loadShaders()
	{
		program = new GLProgram();
		vertShader = new GLVertexShader();
		fragShader = new GLFragmentShader();

		try
		{
			vertShader.compileSources([import("simple.vert")]);
			fragShader.compileSources([import("simple.frag")]);

			program.attachShader(vertShader);
			program.attachShader(fragShader);
			program.link();
			program.use();
		}
		catch(GLException e)
		{
			Stderr("GL Error: ")(program.getInfoLog()).newline;
			Stderr("Ex: ")(e).newline;
		}
	}
	
	protected override void drawGLScene()
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		
		glTranslatef(0.0f, 0.0f, -8.0f);
		
		box.render();
	}

	protected override void keyReleased(int key)
	{
		if(key == SDLK_ESCAPE)
		{
			running = false;
		}
	}

	protected override void keyPressed(int key)
	{
	}
}
